package com.bulletelc.apollopre.manager.imageload;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.manager.imageload.glideloader.GlideImageLoaderStrategy;

/**
 * author: lhl
 * date  : 2017/11/22
 * desc  : 图片加载工具类，对图片加载库进行二次封装，方便切库
 */

public final class ImageLoaderManager implements IImageLoaderStrategy{
    private static ImageLoaderManager instance;
    private IImageLoaderStrategy strategy;

    private ImageLoaderManager(){
        strategy = new GlideImageLoaderStrategy();
    }

    public static ImageLoaderManager getInstance() {
        if (instance == null) {
            synchronized (ImageLoaderManager.class) {
                if (instance == null) {
                    instance = new ImageLoaderManager();
                }
            }
        }
        return instance;
    }

    /**
     * 切换图片加载策略(切库)
     * @param strategy
     */
    public void setStrategy(IImageLoaderStrategy strategy) {
        if (strategy != null) {
            this.strategy = strategy;
        }
    }

    /**
     * 获取默认的Options设置
     * 假如不需要使用ImageView ，自行new一个Image|View传入即可，内部需要通过ImageView获取Context
     * @param container
     * @param url
     * @return
     */
    public static ImageLoaderOptions getDefaultOptions(@NonNull View container, @NonNull String url) {
        return new ImageLoaderOptions.Builder(container, url)
                .placeholder(R.drawable.ic_loadding)
                .error(R.drawable.ic_loading_error)
                .isCrossFade(true)
                .build();
    }

    /**
     * 商品评论图片加载设置
     * @param container
     * @param url
     * @return
     */
    public static ImageLoaderOptions getCommentImgeOptions(@NonNull View container, @NonNull String url) {
        return new ImageLoaderOptions.Builder(container, url)
                .placeholder(R.drawable.shape_loading_placeholder)
                .error(R.drawable.ic_loading_error)
                .isCrossFade(true)
                .build();
    }

    @Override
    public void loadImage(@NonNull ImageLoaderOptions options) {
        strategy.loadImage(options);
    }

    @Override
    public void loadImage(Context context, @NonNull ImageLoaderOptions options) {
        strategy.loadImage(context,options);
    }

    @Override
    public void loadImageWithProgress(String url, ImageView imageView, ImageLoadProgressListener listener) {
        //暂未实现
    }

    @Override
    public void pause(Context context) {
        strategy.pause(context);
    }

    @Override
    public void resume(Context context) {
        strategy.resume(context);
    }

    @Override
    public void clearDiskCache(Context context) {
        strategy.clearDiskCache(context);
    }

    @Override
    public void clearMemoryCache(Context context) {
        strategy.clearMemoryCache(context);
    }

    @Override
    public void clearAllCache(Context context) {
        strategy.clearAllCache(context);
    }
}
